package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.graduate.domain.entity.MsDelog;
import com.ruoyi.graduate.domain.entity.MsFinishedProduct;
import com.ruoyi.graduate.domain.entity.MsPhotographyOrder;
import com.ruoyi.graduate.domain.param.MsFinishedProductAddParam;
import com.ruoyi.graduate.domain.param.MsFinishedProductAuditParam;
import com.ruoyi.graduate.domain.param.MsFinishedProductPageParam;
import com.ruoyi.graduate.domain.param.MsFinishedProductPagePhotoParam;
import com.ruoyi.graduate.domain.vo.MsFinishedProductDetailVo;
import com.ruoyi.graduate.domain.vo.MsFinishedProductListVo;
import com.ruoyi.graduate.domain.vo.MsFinishedProductPageVo;
import com.ruoyi.graduate.mapper.MsDelogMapper;
import com.ruoyi.graduate.mapper.MsFinishedProductMapper;
import com.ruoyi.graduate.mapper.MsPhotographyOrderMapper;
import com.ruoyi.graduate.mapping.MsFinishedProductMapping;
import com.ruoyi.graduate.service.IMsFinishedProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class IMsFinishedProductServiceImpl extends ServiceImpl<MsFinishedProductMapper, MsFinishedProduct>
implements IMsFinishedProductService {

    @Autowired
    private MsFinishedProductMapper msFinishedProductMapper;

    @Autowired
    private MsPhotographyOrderMapper msPhotographyOrderMapper;

    @Autowired
    private MsDelogMapper msDelogMapper;

    @Override
    public PageInfo finishiPage(MsFinishedProductPageParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsFinishedProductPageVo> vo = msFinishedProductMapper.pageList(param);

        return new PageInfo<>(vo);
    }

    @Override
    public PageInfo finishiPagePhoto(MsFinishedProductPagePhotoParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsFinishedProductPageVo> vo = msFinishedProductMapper.pagePhotoList(param);

        return new PageInfo<>(vo);
    }

    @Override
    public AjaxResult detail(Integer id) {

        MsFinishedProductDetailVo detail = msFinishedProductMapper.detail(id);

        return AjaxResult.success(detail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult audit(MsFinishedProductAuditParam param) {
        UpdateWrapper uw = new UpdateWrapper();
        uw.eq("product_id", param.getProductId());
        uw.set("audit_status", param.getAuditStatus());
        uw.set("audit_user_id",param.getAuditUserId());
        uw.set("audit_time", LocalDateTime.now());
        int update = msFinishedProductMapper.update(null, uw);

        if (param.getAuditStatus() == 1){

            MsFinishedProduct msFinishedProduct = msFinishedProductMapper.selectById(param.getProductId());
            UpdateWrapper upw = new UpdateWrapper();
            upw.eq("order_id", msFinishedProduct.getOrderId());
            upw.set("order_status", 3);

            msPhotographyOrderMapper.update(null, upw);

            MsPhotographyOrder byId = msPhotographyOrderMapper.selectById(msFinishedProduct.getOrderId());

            MsDelog delog = new MsDelog();
            delog.setTitle("订单完成");
            delog.setContent("您的订单" + byId.getOrderNo() + "状态为完成");
            delog.setCustomerId(byId.getCustomerId());

            msDelogMapper.insert(delog);

        }

        return update > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(MsFinishedProductAddParam param) {

        MsFinishedProduct add = MsFinishedProductMapping.INSTANCE.add(param);
        add.setUploadTime(LocalDateTime.now());

        int insert = msFinishedProductMapper.insert(add);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult userList(Long userId) {

        QueryWrapper qw = new QueryWrapper();
        qw.eq("customer_id", userId);
        qw.eq("order_status",3);

        List<MsPhotographyOrder> list = msPhotographyOrderMapper.selectList(qw);

        List<MsFinishedProductListVo> vos = new ArrayList<>();

        if (list.size() > 0){

            for (MsPhotographyOrder msPhotographyOrder : list) {
                QueryWrapper upw = new QueryWrapper();
                upw.eq("order_id", msPhotographyOrder.getOrderId());
                upw.eq("audit_status", 1);

                MsFinishedProduct msFinishedProduct = msFinishedProductMapper.selectOne(upw);

                if (msFinishedProduct != null){

                    MsFinishedProductListVo list1 = MsFinishedProductMapping.INSTANCE.list(msFinishedProduct);
                    vos.add(list1);
                }
            }

        }
        return AjaxResult.success(vos);
    }
}
